package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.FeignClientItem;
import com.hmall.common.FeignClientMy;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.User;
import com.hmall.order.config.TreadLocalMain;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private FeignClientMy feignClientMy;

    @Autowired
    private FeignClientItem feignClientItem;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @GlobalTransactional
    public long saveOrder(OrderDto orderDto){
        Order order = null;
        try {
            order = new Order();
            //用户id
            String id = TreadLocalMain.getname();
            order.setUserId(Long.parseLong(id));
            User user = feignClientMy.findById(Long.parseLong(id));
            Address address = feignClientMy.findAddressById(orderDto.getAddressId());
            //设置支付方式
            order.setPaymentType(orderDto.getPaymentType());
            //设置金额
            Integer itemId = Math.toIntExact(orderDto.getItemId());
            System.out.println(itemId);
            Item item = feignClientItem.getItem(itemId);
            long l = item.getPrice() * orderDto.getNum();
            order.setTotalFee(l);
            //订单状态
            order.setStatus(1);

            int insert = orderMapper.insert(order);

            //库存减少，**事务安全

            feignClientItem.Stock(orderDto.getItemId(),orderDto.getNum());

            testTTLQueue(orderDto.getNum(),orderDto.getItemId(),Long.parseLong(TreadLocalMain.getname()),order.getId());
            return order.getId();
        } catch (NumberFormatException e) {

            log.error(e.getLocalizedMessage());
        } finally {

            log.debug("死信发送");
            return order.getId();
        }



    }
    public void testTTLQueue(long num,long ItemId,long id,long orderId) {
        // 创建消息

        Map<String,Long> map = new HashMap<>();
        map.put("id",id);
        map.put("ItemId",ItemId);
        map.put("num",num);
        map.put("orderId",orderId);

        // 消息ID，需要封装到CorrelationData中
        CorrelationData correlationData = new CorrelationData("aaaaa");
        // 发送消息
        rabbitTemplate.convertAndSend("ttl.direct", "ttl", map, correlationData);
        // 记录日志
        log.debug("发送消息成功");
    }
}
